var Signin = {
  config: {
    attach: []
  },
  init: function (config) {
    var _this = this;
    $.extend(_this.config, config);
    _this.init_btn();

    if( _this.config.user_info.subscribe == 0 ) {
      ios_alert.init('请先关注微信号' , function(){
        window.location = _this.config.event_data.bootstrap_url ;
      });
    }
  },
  
  set_attach: function (data) {
    var _this = this;
    //_this.config.attach.push(data);
    var html = '<div class="col-xs-4 img-item">';
    html += '<a class="del-item-btn" href="#">&times;</a>';
    html += '<img src="' + data.ab_path + '" class="img-thumbnail">';
    html += '</div>';

    $('.img-preview').append(html);
    _this.config.attach.push(data);
  },
  
  init_btn: function () {
    var _this = this;

    //上传图片
    $(document).on('click', '#img_upload_btn', function (e) {
      e.preventDefault();
      
      if (_this.config.attach.length >= 3) {
        tips('最多上传3张图片');
        return;
      }
      $('#file_upload_1-button').trigger('click');
    });
    
    //封面
    $('#real_img_upload_btn').Huploadify({
      uploader : _this.config.upload_icon_url , //上传文件
      itemTemplate : '',
      onUploadSuccess : function(file, responseText){
        var ret = $.parseJSON( responseText );
        tips( ret.info );
        if ( ret.status ) {
          _this.set_attach( ret.data );
        }
      }
    });



    //删除图片
    $(document).on('click', '.del-item-btn', function (e) {
      e.preventDefault();
      var index = $(this).index('.del-item-btn');

      var savePath = _this.config.attach[index].savePath
      _this.config.attach.remove(index);
      $('.img-item').eq(index).remove();

      var url = _this.config.del_icon_url;
      var data = {savePath: savePath};
      $.post(url, data, function () {
      });
    });

    //提交
    $(document).on('click', '#do_signin_btn', function (e) {
      e.preventDefault();

      var data = $('#signin_form').serializeObject();
      data.attach = _this.config.attach ;
      var ret = _this.check_data(data);

      if (!ret.status) {
        tips(ret.info);
        return;
      }

      if (_this.config.attach.length < 1) {
        tips('请先上传图片');
        return;
      }

      var url = _this.config.do_signin_url;

      $.post(url, data, function (ret) {
        if (ret.status) {
          ios_alert.init( ret.info , function(){
            window.location.replace( _this.config.detail_url + ret.id + _this.config.url_query_string );
          });
        } else {
          tips(ret.info);
        }
      });
    });
  },
  
  //检查表单
  check_data: function (data) {
    //z(data);
    if (data['truename'].length < 2 || data['truename'].length > 15) {
      return {status: false, info: '姓名需2-15个字'};
    }

    var telReg = !!data.phone.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/);

    //如果手机号码不能通过验证
    if (telReg == false) {
      return {status: false, info: '电话号码不正确'};
    }

    if (data['desc'].length > 100 || data['desc'].length < 2) {
      return {status: false, info: '简介3-100个字'};
    }

    return {status: true, info: 'ok'};
  },
  


}